/*
 * Copyright Iain Sproat, 2008
 * 
 *
 */

using System;
using NUnit.Framework;
using Rhino.Mocks;

using FreeBase.Fluent;
using FreeBase.Fluent.MQLConverter;

namespace FreeBase_Test.Fluent.MQLConverter
{
	[TestFixture]
	public class ExamplesFromCheatSheet
	{
		[Test]
		public void RunPoliceTrackExample()
		{
			
			FBQuery ThePolice_TrackNames_BeginningWith_Message =
				Freebase.Query
				.Where().Name.Is("The Police")
				.Where().Type.Is("/music/artist")
				.Expect.Many("track").Which.Meets(
					Freebase.Query
					.Show.OneRecordFor.AllProperties
					.Where().Name.StartsWith("Message")
					.LimitNumberOfResults.To(1)
				);
			
			Assert.IsNotNull( ThePolice_TrackNames_BeginningWith_Message );
			
			IMQLConverter SUT = new FluentMQLConverter();
			string ret = SUT.FluentToMQL( ThePolice_TrackNames_BeginningWith_Message );
			string expected = "{\"query\":" +
				"[{\"name\":\"The Police\"," +
				"\"type\":\"/music/artist\"," +
				"\"track\":[{" +
				"\"*\":null," +
				"\"name~=\":\"^Message\"," +
				"\"limit\":1" +
				"}]}]}";
			Console.WriteLine( "Can Reproduce Example => " + ret );
			Assert.AreEqual( expected, ret);
		}
		
		[Test]
		public void CanANDCriteria()
		{
			
			FBQuery ThePolice_TrackNames_BeginningWithMessage_And_ContainingBottle =
				Freebase.Query
				.Where().Name.Is("The Police")
				.Where().Type.Is("/music/artist")
				.Expect.Many("track").Which.Meets(
					Freebase.Query
					.Show.OneRecordFor.AllProperties
					.Where().Name.StartsWith("Message")
					.Where().Name.Contains("Bottle")
					.LimitNumberOfResults.To(1)
				);
			
			Assert.IsNotNull( ThePolice_TrackNames_BeginningWithMessage_And_ContainingBottle );
			
			IMQLConverter SUT = new FluentMQLConverter();
			string ret = SUT.FluentToMQL( ThePolice_TrackNames_BeginningWithMessage_And_ContainingBottle );
			string expected = "{\"query\":" +
				"[{\"name\":\"The Police\"," +
				"\"type\":\"/music/artist\"," +
				"\"track\":[{" +
				"\"*\":null," +
				"\"criteria0:name~=\":\"^Message\"," +
				"\"criteria1:name~=\":\"Bottle\"," +
				"\"limit\":1" +
				"}]}]}";
			Console.WriteLine( "And Test => " + ret );
			Assert.AreEqual( expected, ret);
		}
		
		[Test]
		public void CanOrCriteria()
		{
			
			FBQuery ThePolice_TrackNames_WhichAre_MessageInABottle_Or_EveryBreathYouTake =
				Freebase.Query
				.Where().Name.Is("The Police")
				.Where().Type.Is("/music/artist")
				.Expect.Many("track").Which.Meets(
					Freebase.Query
					.Show.OneRecordFor.AllProperties
					.Where().Name.Is("Message In A Bottle",
					                 "Every Breath You Take") //This is an OR statement
					.LimitNumberOfResults.To(2)
				);
			
			Assert.IsNotNull( ThePolice_TrackNames_WhichAre_MessageInABottle_Or_EveryBreathYouTake );
			
			IMQLConverter SUT = new FluentMQLConverter();
			string ret = SUT.FluentToMQL( ThePolice_TrackNames_WhichAre_MessageInABottle_Or_EveryBreathYouTake );
			string expected = "{\"query\":" +
				"[{\"name\":\"The Police\"," +
				"\"type\":\"/music/artist\"," +
				"\"track\":[{" +
				"\"*\":null," +
				"\"name|=\":[\"Message In A Bottle\",\"Every Breath You Take\"]," +
				"\"limit\":2" +
				"}]}]}";
			Console.WriteLine( "OR test => " + ret );
			Assert.AreEqual( expected, ret);
		}
	}
}
